package com.cmcc.aregister.ui.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmcc.aregister.MyApplication;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.lazyimage.LazyImageView;
import com.cmcc.aregister.cs.model.Hospital;

public class ChoiceHospitalAdapter extends BaseAdapter {
    private Context context = null;
    public List<Hospital> list = null;
    private Handler handler;
    public int currPage;
    public int allPage;
    private boolean isLocal;

    public ChoiceHospitalAdapter(Context context, List<Hospital> list,
        int currPage, int allPage, Handler handler, boolean isLocal) {
        this.context = context;
        this.list = list;
        this.allPage = allPage;
        this.handler = handler;
        this.isLocal = isLocal;
        this.currPage = currPage;
    }

    public int getCount() {
        int size = list.size();
        if (allPage == 0) {
            allPage = 1;
        }
        if (currPage == 0) {
            currPage = 1;
        }
        if (allPage > currPage) {
            return size + 1;
        }
        else {
            return size;
        }
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        if (position >= list.size()) {
            return false;
        }
        else {
            return true;
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (position >= list.size()) {
            handler.sendMessage(handler.obtainMessage(-1, position));
            convertView = LayoutInflater.from(context).inflate(
                R.layout.list_wait, null);
            convertView.setTag(null);
            convertView.setEnabled(false);
            return convertView;
        }
        else {
            ViewHolder holder;
            if (convertView != null && convertView.getTag() != null) {
                holder = (ViewHolder) convertView.getTag();
            }
            else {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                    R.layout.a_choice_hospital, null);
                holder.image = (LazyImageView) convertView
                    .findViewById(R.id.image);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.level = (TextView) convertView.findViewById(R.id.level);
                holder.yuyue_num = (TextView) convertView
                    .findViewById(R.id.yuyue_num);
                holder.address = (TextView) convertView
                    .findViewById(R.id.address);
                holder.detail = (Button) convertView.findViewById(R.id.detail);
                holder.detail.setOnClickListener(onClickListener);
                holder.attention_layout = (LinearLayout) convertView
                    .findViewById(R.id.attention_layout);
                convertView.setTag(holder);
            }
            Hospital hospital = list.get(position);
            holder.detail.setTag(position);
            holder.image.loadImage(hospital.image, 0, 0);
            holder.name.setText(hospital.unit_name);
            if (isLocal) {
                holder.level.setText(hospital.unit_level + "  "
                    + context.getString(R.string.left_num));
                holder.yuyue_num.setText(hospital.left_num);
            }
            else {
                holder.level.setText(hospital.unit_level);
            }
            holder.address.setText(hospital.address);
            String is_attention = MyApplication.getInstance().hospitalMap
                .get(hospital.unit_id);
            if (is_attention == null || "".equals(is_attention)) {
                is_attention = hospital.is_attention;
            }
            if ("1".equals(is_attention)) {
                holder.attention_layout.setVisibility(View.VISIBLE);
            }
            else {
                holder.attention_layout.setVisibility(View.GONE);
            }
            return convertView;
        }
    }

    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            handler.sendMessage(handler.obtainMessage(0, v.getTag()));
        }
    };

    static class ViewHolder {
        LazyImageView image;
        TextView address;
        TextView name;
        TextView level, yuyue_num;
        Button detail;
        LinearLayout attention_layout;
    }
}